package ar.edu.itba.it.paw.web.product;

import java.util.Date;
import java.util.List;

import org.apache.taglibs.standard.lang.jstl.MinusOperator;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.parse.metapattern.parsers.IntegerVariableAssignmentParser;
import org.apache.wicket.validation.validator.MinimumValidator;
import org.apache.wicket.validation.validator.StringValidator;

import ar.edu.itba.it.paw.domain.Auction;
import ar.edu.itba.it.paw.domain.Category;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.repo.CategoryRepo;
import ar.edu.itba.it.paw.repo.ProductRepo;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.MercadoLibreSession;
import ar.edu.itba.it.paw.web.base.BasePage;
import ar.edu.itba.it.paw.web.user.UserMenuPage;

public class AddProductPage extends BasePage{

	@SpringBean
	CategoryRepo categoryRepo;
	@SpringBean
	UserRepo userRepo;
	@SpringBean
	ProductRepo prodRepo;
	
	private String name;
	private String description;
	private Integer quantity;
	private float price;
	private transient Category category;
	private boolean state;
	private boolean auction;
	
	public AddProductPage(){
		add(new FeedbackPanel("feedback").add(new SimpleAttributeModifier("style", "color: #FF0000")));
		Form<AddProductPage> form = new Form<AddProductPage>("addProductForm", new CompoundPropertyModel<AddProductPage>(this)) {
			@Override
			protected void onSubmit() {
				User user = MercadoLibreSession.get().getUser();
				if(auction){
					prodRepo.addProduct(new Auction(name,description,new Date(),category,price,quantity,state,user));
				}else{
					prodRepo.addProduct(new Product(name,description,new Date(),category,price,quantity,state,user));
				}
				setResponsePage(UserMenuPage.class);
			}
		};

		form.add(new TextField<String>("name").setRequired(true).add(StringValidator.maximumLength(50)));
		form.add(new TextField<String>("description").setRequired(true).add(StringValidator.maximumLength(200)));
		form.add(new TextField<Integer>("quantity").setRequired(true).add(new MinimumValidator<Integer>(1)));
		form.add(new TextField<Float>("price").setRequired(true).add(new MinimumValidator<Float>(new Float(0.1))));
		IModel<List<Category>> categoriesModel = new LoadableDetachableModel<List<Category>>() {
			@Override
			protected List<Category> load() {
				return categoryRepo.getCategories();
			}
		};
		form.add(new DropDownChoice<Category>("category", categoriesModel).setRequired(true));
		form.add(new CheckBox("state"));
		form.add(new CheckBox("auction"));
		form.add(new Button("add", new ResourceModel("add")));
		add(form);
	}
}
